﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SharpSphinx.Util.Configuration;



namespace SharpSphinx.Util.Configuration
{
    
    public class RawPropertyData
    {

        private String name;
        private String className;
        private Dictionary<String, Object> properties;


        /// <summary>
        /// Creates a raw property data item.
        /// </summary>
        /// <param name="name">the name of the item</param>
        /// <param name="className">the class name of the item</param>

        public RawPropertyData(String name, String className)
            : this(name,className,new Dictionary<string,object>())
        {

        }


        /// <summary>
        /// Creates a raw property data item, using a given property map.
        /// </summary>
        /// <param name="name">the name of the item</param>
        /// <param name="className">the class name of the item</param>
        /// <param name="properties">existing property map to use</param>
        public RawPropertyData(String name, String className,Dictionary<String,Object> properties)
        {
            this.name = name;
            this.className = className;
            this.properties = properties;
        }


        /// <summary>
        /// Adds a new property with a {@code List<String>} value.
        /// </summary>
        /// <param name="propName">the name of the property</param>
        /// <param name="propValue">the value of the property</param>
        public void Add(String propName, List<string> propValue)
        {

            properties.Add(propName, propValue);
        }


        /// <summary>
        /// Removes an existing property.
        /// </summary>
        /// <param name="propName">the name of the property</param>
        public void Remove(string propName)
        {
            properties.Remove(propName);
        }

        /// <summary>
        ///  Returns the className.
        /// </summary>
        /// <returns> Returns the className.</returns>
        public String ClassName
        {
            get { return className; }
        }


        /// <summary>
        /// return Returns the name
        /// </summary>
        /// <returns>return Returns the name</returns>
        public String Name
        {
            get { return name; } 
        }


        /// <summary>
        /// Returns the properties.
        /// </summary>
        /// <returns>Returns the properties.</returns>
        public Dictionary<String, Object> Properties
        {
            get { return properties; }
        }

        /// <summary>
        /// Determines if the map already contains an entry for a property.
        /// </summary>
        /// <param name="propName"></param>
        /// <returns></returns>
        public bool Contains(String propName)
        {
            return properties.ContainsValue(propName); 

        }


        /// <summary>
        /// Returns a copy of this property data instance with all ${}-fields resolved.
        /// </summary>
        /// <param name="cm"></param>
        /// <returns></returns>
        public RawPropertyData flatten(ConfigurationManager cm)
        {
            RawPropertyData copyRPD = new RawPropertyData(name, className);

            foreach (KeyValuePair<String, Object> Entry in properties)
            {
                Object Prop = Entry.Value;
                if (Prop is String)
                {
                    if (((String)Prop).StartsWith("${"))
                    {
                        // Prop = cm.GetPropReference
                    }
                          
                }


            }
            return copyRPD;
        }



        /// <summary>
        /// Lookup a global symbol with a given name (and resolves
        /// </summary>
        /// <param name="key"> the name of the property</param>
        /// <param name="globalProperties"></param>
        /// <returns>the property value or null if it doesn't exist.</returns>
        public String getGlobalProperty(String key,Dictionary<String,String> globalProperties)
        {
            if (!key.StartsWith("${"))
                return key;


            while (true)
            {
                 globalProperties.TryGetValue(key,out key);
                    if (key == null || (key.StartsWith("${") && key.EndsWith("}")))
                           return key;
            }
        


        }


        /// <summary>
        /// Provide information stored inside this Object, used mainly for debugging/testing.
        /// </summary>
        /// <returns>Description of object</returns>
        public override string ToString()
        {
         
            StringBuilder output = new StringBuilder().Append("name : ").Append(name); 
             foreach (Object value in properties.Values)
             {
                 if (value != null)
                 {
                     if (value is String)
                     {
                         output.Append("Value string: "); 
                     }

                     output.Append(value);
                 }
             }

             return output.ToString();
           
        }
    }
}
